/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package cart.hibernate.movieInformation;

/**
 *
 * @author BibooChung
 */
import java.util.*;
import cart.hibernate.*;

import cart.hibernate.HibernateUtil;
import cart.hibernate.rating.*;
import cart.hibernate.releaseCompany.*;
import cart.hibernate.genre.*;
import cart.hibernate.actor.*;
import cart.hibernate.coverImage.*;


import java.util.List; 
import java.util.ArrayList;
import java.util.Iterator; 
import java.util.Set;
import java.util.HashSet;
 
import org.hibernate.HibernateException; 
import org.hibernate.Session; 
import org.hibernate.Transaction;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class ManageMovieInformation {
    /*public static void main(String[] args) {
        Date date = new Date();
        
        List list = new ArrayList();
        list.add(1);
        List<Long> listLong = new ArrayList<Long>();
        listLong.add((long)1);
        ManageMovieInformation manager = new ManageMovieInformation();
        manager.updateMovieRating(29, 1010);
        
    }*/
 
    public List getAllMovie() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = null;
        List allMovie = null;
        try{
           tx = session.beginTransaction();
           allMovie = session.createQuery("FROM MovieInformation").list(); 
           tx.commit();
        }catch (HibernateException e) {
           if (tx!=null) tx.rollback();
           e.printStackTrace(); 
        }finally {
           session.close(); 
        }
        return allMovie;
    }
    
    public static Set covertGenreIdToGenreSet(List idList, Session session){
        Set set = new HashSet();
        ArrayList targetIdList = new ArrayList(idList);           
        for (int i = 0 ; i < targetIdList.size() ; i++)
        {
          int id = (Integer)(targetIdList.get(i));
          Genre targetObject = (Genre)session.get(Genre.class, id);
          set.add(targetObject);
        }
      return set;
    }

    public static Set covertActorIdToActorSet(List idList, Session session){
        Set set = new HashSet();
        ArrayList targetIdList = new ArrayList(idList);           
        for (int i = 0 ; i < targetIdList.size() ; i++)
        {
          long id = (Long)(targetIdList.get(i));
          Actor targetObject = new Actor((Actor)session.get(Actor.class, id));
          set.add(targetObject);
        }
      return set;
    }

    public static Set covertImageIdToImageSet(List idList, Session session){
        Set set = new HashSet();
        ArrayList targetIdList = new ArrayList(idList);           
        for (int i = 0 ; i < targetIdList.size() ; i++)
        {
          long id = (Long)(targetIdList.get(i));
          CoverImage targetObject = new CoverImage((CoverImage)session.get(CoverImage.class, id));
          set.add(targetObject);
        }
      return set;
    }
    
    public long addMovie(String movieName, double unitPrice, double discount, 
                            int stockQuantity, Date releaseDate, Date productAvailableDate, 
                            String productDescription, int releaseCompanyId,
                            List genreId, List actorId, List imageId) { 
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = null;
        long movieId = -1;

        try {
            tx = session.beginTransaction();

            ReleaseCompany releaseCompany = (ReleaseCompany)session.get(ReleaseCompany.class, releaseCompanyId); 
            Rating rating = new Rating(0.0, (long) 0);

            Set<Genre> genres;
            Set<Actor> actors;
            Set<CoverImage> images;
            
            genres = covertGenreIdToGenreSet(genreId, session);
            actors = covertActorIdToActorSet(actorId, session);
            images = covertImageIdToImageSet(imageId, session);

            MovieInformation movie = new MovieInformation(movieName, unitPrice, discount, 
                            stockQuantity, releaseDate, productAvailableDate, 
                            productDescription, releaseCompany, rating);

            movie.getGenres().clear();
            movie.getGenres().addAll(genres);

            movie.getActors().clear();
            movie.getActors().addAll(actors);

            movie.getImages().clear();
            movie.getImages().addAll(images);

            rating.setMovie(movie);

            movieId = (Long) session.save(movie);
            
            for (CoverImage image : images)
            {
              image.setMovie(movie);
              session.save(image);
            }

            tx.commit();

        } catch (HibernateException e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        } finally {
            session.close();
        }
        return movieId;
    }
    
    public void updateMovie(long movieId, String movieName, double unitPrice, double discount, 
                            int stockQuantity, Date releaseDate, Date productAvailableDate, 
                            String productDescription, int releaseCompanyId,
                            List genreId, List actorId, List imageId) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = null;
        try{
            tx = session.beginTransaction();

            ReleaseCompany releaseCompany = (ReleaseCompany)session.get(ReleaseCompany.class, releaseCompanyId); 
            Rating rating = (Rating)session.get(Rating.class, movieId); 

            Set<Genre> genres;
            Set<Actor> actors;
            Set<CoverImage> images;
            
            genres = covertGenreIdToGenreSet(genreId, session);
            actors = covertActorIdToActorSet(actorId, session);
            images = covertImageIdToImageSet(imageId, session);

            MovieInformation movie = (MovieInformation)session.get(MovieInformation.class, movieId); 

            movie.setMovieInformation(movieName, unitPrice, discount, 
                            stockQuantity, releaseDate, productAvailableDate, 
                            productDescription, releaseCompany, rating);

            movie.getGenres().clear();
            movie.getGenres().addAll(genres);

            movie.getActors().clear();
            movie.getActors().addAll(actors);

            movie.getImages().clear();
            movie.getImages().addAll(images);

            rating.setMovie(movie);

         //   movieId = (Long) session.save(movie);
            session.update(movie);
            for (CoverImage image : images)
            {
              image.setMovie(movie);
              session.save(image);
            }

            
            tx.commit();
        }catch (HibernateException e) {
           if (tx!=null) tx.rollback();
           e.printStackTrace(); 
        }finally {
           session.close(); 
        }
    }

    public void updateMovieRating(long movieId, double newRate) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = null;
        try{
            tx = session.beginTransaction();

            Rating rating = (Rating)session.get(Rating.class, movieId); 
            MovieInformation movie = (MovieInformation)session.get(MovieInformation.class, movieId); 

            rating.addRate(newRate);

            rating.setMovie(movie);
            session.update(movie);
            
            tx.commit();
        }catch (HibernateException e) {
           if (tx!=null) tx.rollback();
           e.printStackTrace(); 
        }finally {
           session.close(); 
        }
    }

    public void deleteMovie(long movieId) {
      Session session = HibernateUtil.getSessionFactory().openSession();
      Transaction tx = null;
      try{
         tx = session.beginTransaction();
         MovieInformation movie = (MovieInformation)session.get(MovieInformation.class, movieId); 
         session.delete(movie); 
         tx.commit();
      }catch (HibernateException e) {
         if (tx!=null) tx.rollback();
         e.printStackTrace(); 
      }finally {
         session.close(); 
      }
    }
 
        
 
 
    
}
